<template>
  <div class="paper-warp">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <template v-if="paperType === 'paperHeader'">
        <el-form-item label="试卷名称" prop="paperName">
          <el-input v-model="form.paperName" maxlength="20" @change="changePaperInput($event, 'paperName')"></el-input>
        </el-form-item>
        <el-form-item label="试卷描述" prop="paperDes">
          <el-input v-model="form.paperDes" maxlength="100" @change="changePaperInput($event, 'paperDes')"></el-input>
        </el-form-item>
        <el-form-item label="注意事项" prop="paperAttention">
          <el-input v-model="form.paperAttention" maxlength="100" @change="changePaperInput($event, 'paperAttention')"></el-input>
        </el-form-item>
        <el-form-item label="考试时长" prop="paperTime">
          <el-input-number
            v-model="form.paperTime"
            style="width: 140px"
            :min="1"
            :max="500"
            :precision="0"
            @change="changePaperInput($event, 'paperTime')"
          ></el-input-number>
          <span>分钟</span>
        </el-form-item>
      </template>

      <template v-else-if="paperType === 'bigQuestion'">
        <el-form-item label="试卷名称" prop="bigName">
          <el-input v-model="form.bigName" maxlength="10" @change="changeQuestionInput($event, 'bigName')"></el-input>
        </el-form-item>
        <el-form-item label="详细描述" prop="bigDesc">
          <el-input v-model="form.bigDesc" maxlength="20" @change="changeQuestionInput($event, 'bigDesc')"></el-input>
        </el-form-item>
      </template>

      <template v-else-if="paperType === 'question'">
        <el-form-item label="小题满分" prop="score">
          <el-input-number
            v-model="form.score"
            style="width: 140px"
            :min="1"
            :max="500"
            :precision="0"
            @change="changeQuestionInput($event, 'score')"
          ></el-input-number>
          <span></span>
        </el-form-item>
      </template>
    </el-form>
  </div>
</template>
<script>
export default {
  components: {},
  props: {
    paperType: {
      type: String,
      default: "",
    },
    paperData: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      form: {},
      rules: {
        paperName: [{ required: true, message: "请输入试卷名称", trigger: "blur" }],
        paperDes: [{ required: true, message: "请输入试卷描述", trigger: "blur" }],
        paperAttention: [{ required: true, message: "请输入注意事项", trigger: "blur" }],
        paperTime: [
          {
            required: true,
            message: "请输入考试时长",
            trigger: ["blur", "change"],
          },
        ],

        bigName: [{ required: true, message: "请输入大题名称", trigger: "blur" }],
        bigDesc: [{ required: true, message: "请输入详细描述", trigger: "blur" }],
      },
    };
  },
  watch: {
    paperData(data) {
      this.renderData(data);
    },
  },
  created() {
    this.renderData(this.paperData);
  },
  methods: {
    renderData(data) {
      this.form = {
        ...data,
      };
    },
    changePaperInput(e, type) {
      let attrs = {};
      attrs[type] = e;
      this.$emit("changePaper", attrs);
    },
    changeQuestionInput(e, type) {
      let attrs = {};
      attrs[type] = e;
      this.$emit("changeQuestion", {
        optionId: this.form.optionId,
        attrs: attrs,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.paper-warp {
  padding: 20px 30px;
}
</style>
